{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试开始时间: 2022-05-14 11:40:13.876450\n",
      "前车的初速度：67.274\n",
      "后车的初速度：77.251\n",
      "两辆车的初始时间间距：0.431s\n",
      "前车的刹车加速度：3.595\n",
      "前车的刹车时刻：13.587\n",
      "后车的反应时间：0.415\n",
      "后车的刹车加速度：24.038\n",
      "前车的停车位置：1080.603\n",
      "后车的停车位置：1009.212\n",
      "两车停车时，两车相距：71.392,两车不会追尾\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "import random\n",
    "\n",
    "'''测试时间'''\n",
    "text_time = datetime.datetime.now()\n",
    "print('测试开始时间: '+str(text_time))\n",
    "\n",
    "'''线性同余发生器生成一个0-1的基本随机数列表'''\n",
    "def random_1(a, m):\n",
    "    road_basicdatas_0 = [17]\n",
    "    road_basicdatas = []\n",
    "    for x in range(1, 250):\n",
    "        road_basicdatas_0.append(a * road_basicdatas_0[x - 1] % m)\n",
    "        road_basicdatas.append(a * road_basicdatas_0[x - 1] % m / m)  # 得到精确到万分位的0-1随机小数\n",
    "\n",
    "    return road_basicdatas  # 最终生成的0-1的随机数列表并返回\n",
    "\n",
    "\n",
    "'''产生两个车的初始时间间距'''\n",
    "def space_between():\n",
    "    space_between_1 = random_1(25,1024)\n",
    "    space_between_0 = []\n",
    "    for i in range(len(space_between_1)):\n",
    "        space_between_0.append(space_between_1[i] )     #两车的初始时间间隔0-1s左右\n",
    "\n",
    "    return space_between_0\n",
    "\n",
    "'''两车的初速度'''\n",
    "car_speed = random_1(25,1024)\n",
    "car_speed_1 = car_speed[random.randint(4,100)] + random.randint(60,80) #第一辆车的车速\n",
    "car_speed_2 = car_speed[random.randint(4,100)] + random.randint(60,80) #第二辆车的车速\n",
    "\n",
    "'''后车的反应时间'''\n",
    "for i in range(len(random_1(25,1024))):\n",
    "    if random_1(25,1024)[i] >= 0.3 and random_1(25,1024)[i] <= 1.0:   #限定后车反应时间[0.3,1.0]\n",
    "        back_car_reaction = random_1(25,1024)[i]\n",
    "        break\n",
    "\n",
    "'''前车的刹车加速度和刹车时间、后车刹车加速度'''\n",
    "plus_speed_1 = random_1(25,1024)[random.randint(20,100)] + random.randint(3,8)   #前车刹车加速度\n",
    "stop_time_1 = random_1(25,1024)[random.randint(20,100)] + random.randint(10,20)   #前车开始刹车时间\n",
    "plus_speed_2 = abs((car_speed_1 - car_speed_2)/back_car_reaction)    #后车刹车加速度\n",
    "\n",
    "\n",
    "'''前车的停车位置'''\n",
    "a = space_between()[30]     #两车的初始间距\n",
    "stop_car_place_1 = car_speed_1 * stop_time_1 +(car_speed_1)**2/(2*stop_time_1)\n",
    "stop_car_place_2 = car_speed_1 * (stop_time_1-a)+(car_speed_2)**2/(2*plus_speed_2)   #前后两车同时刹车\n",
    "\n",
    "\n",
    "'''各种输出'''\n",
    "print('前车的初速度：'+str(round(car_speed_1,3)))\n",
    "print('后车的初速度：'+str(round(car_speed_2,3)))\n",
    "print('两辆车的初始时间间距：'+str(round(a,3))+'s')\n",
    "print('前车的刹车加速度：'+str(round(plus_speed_1,3)))\n",
    "print('前车的刹车时刻：'+str(round(stop_time_1,3)))\n",
    "print('后车的反应时间：'+str(round(back_car_reaction,3)))\n",
    "print('后车的刹车加速度：'+str(round(plus_speed_2,3)))\n",
    "print('前车的停车位置：'+str(round(stop_car_place_1,3)))\n",
    "print('后车的停车位置：'+str(round(stop_car_place_2,3)))\n",
    "\n",
    "if stop_car_place_2 < stop_car_place_1:\n",
    "    print('两车停车时，两车相距：'+str(round(stop_car_place_1-stop_car_place_2,3))+',两车不会追尾')\n",
    "else:\n",
    "    print('两车刹车时距离过近，发生追尾')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}